package com.example.springbott.wx_controller;

import com.example.springbott.bean.BaseRespVo;
import com.example.springbott.bean.MarketAddress;
import com.example.springbott.bean.MarketUser;
import com.example.springbott.bean.zeng.BaseRespVoInDelete;
import com.example.springbott.common.CommonData;
import com.example.springbott.util.Count;
import com.example.springbott.wx_service.WXAddressService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: zeng
 * @Description:
 * @date: 15:14 2022/11/22
 */
@RestController
@RequestMapping("wx/address")
public class WXAddressController {
    @Autowired
    WXAddressService wxAddressService;

    @RequestMapping("list")
    public BaseRespVo list() {
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        // Subject subject = SecurityUtils.getSubject();
        // MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        // MarketUser marketUser = new MarketUser();
        if (marketUser == null) {
            return BaseRespVo.fail("需要重新登陆", Count.PLEASELOGIN);
        }
        CommonData<MarketAddress> data = wxAddressService.list(marketUser);

        return BaseRespVo.ok(data);

    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        MarketAddress marketAddress = wxAddressService.detail(id);
        return BaseRespVo.ok(marketAddress);
    }

    @RequestMapping("save")
    public BaseRespVo save(@RequestBody MarketAddress address) {
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (marketUser == null) {
            return BaseRespVo.fail("需要重新登陆", Count.PLEASELOGIN);
        }
        Integer id = wxAddressService.save(address, marketUser);
        return BaseRespVo.ok(id);
    }

    @RequestMapping("delete")
    public BaseRespVoInDelete delete(@RequestBody Map map) {
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (marketUser == null) {
            return BaseRespVoInDelete.fail("需要重新登陆", Count.PLEASELOGIN);
        }
        Integer id = (Integer) map.get("id");
        wxAddressService.delete(id);
        return BaseRespVoInDelete.ok();
    }
}
